/*
 * Copyright Hundsun Technologies Inc. All Rights Reserved.
 */

package db

const (
	// PrefixSeparator 前缀分隔符
	PrefixSeparator = "-"
	// NamespaceAccount account数据前缀
	NamespaceAccount = "ns.ac" + PrefixSeparator
	// NamespaceLedgerBlock ...
	NamespaceLedgerBlock = "ns.ld.bl" + PrefixSeparator
	// NamespaceLedgerBlockExecResult ...
	NamespaceLedgerBlockExecResult = "ns.ld.blexecrs" + PrefixSeparator
	// NamespaceLedgerExecutedBlock ...
	NamespaceLedgerExecutedBlock = "ns.ld.execbl" + PrefixSeparator
	// NamespaceLedgerExecutedTransaction ...
	NamespaceLedgerExecutedTransaction = "ns.ld.exectx" + PrefixSeparator
	// NamespaceLedgerTopicInfo ...
	NamespaceLedgerTopicInfo = "ns.ld.topic" + PrefixSeparator
	// NamespaceLedgerSequenceBlock ...
	NamespaceLedgerSequenceBlock = "ns.ld.seqblk" + PrefixSeparator
	// NamespaceLedgerRoundMinerInfo ...
	NamespaceLedgerRoundMinerInfo = "ns.ld.rd" + PrefixSeparator
	// NamespaceContract ...
	NamespaceContract = "ns.ct" + PrefixSeparator
	// NamespaceContractVersion ...
	NamespaceContractVersion = "ns.ct.ver" + PrefixSeparator
	// NamespaceContractStatus ...
	NamespaceContractStatus = "ns.ct.status" + PrefixSeparator
	// NamespaceContractDao ...
	NamespaceContractDao = "ns.ct.dao" + PrefixSeparator
	// NamespaceContractEvent ...
	NamespaceContractEvent = "ns.ct.event" + PrefixSeparator
	// NamespaceManageFlowID ...
	NamespaceManageFlowID = "ns.mf.id" + PrefixSeparator
	// NamespaceManageFlowCfg ...
	NamespaceManageFlowCfg = "ns.mf.cfg" + PrefixSeparator
	// NamespaceManageFlowStatus ...
	NamespaceManageFlowStatus = "ns.mf.status" + PrefixSeparator
	// NamespaceManageFlowGroupInfo ...
	NamespaceManageFlowGroupInfo = "ns.mf.gp" + PrefixSeparator
	// NamespaceP2pPeerInfo ...
	NamespaceP2pPeerInfo = "ns.p2p.peerInfo" + PrefixSeparator
	// NamespaceP2pMsg ...
	NamespaceP2pMsg = "ns.p2p.msg" + PrefixSeparator
	// NamespaceRPCBlkNum ...
	NamespaceRPCBlkNum = "ns.rpc.blkNum" + PrefixSeparator
	// NamespaceSubscriber ...
	NamespaceSubscriber = "ns.sub" + PrefixSeparator
	// NamespaceSystemParam 系统参数存储前缀
	NamespaceSystemParam = "ns.sys.param" + PrefixSeparator
	// DefaultIndexName 默认表/存储名称
	DefaultIndexName = "ns.statedb"
	// DefaultNamespace ...
	DefaultNamespace = DefaultIndexName + PrefixSeparator
)

// DatabaseGet get
type DatabaseGet interface {
	Get(key []byte) ([]byte, error)
	// Gets批量获取, 获取出来的key, value必须一一对应，如果不存在则空着
	Gets(keys [][]byte) ([][]byte, error)
}

// Database 数据库的接口
type Database interface {
	DatabaseGet
	Set(key []byte, value []byte) error
	Has(key []byte) (bool, error)
	Delete(key []byte) error
	Close()
	NewBatch(sync bool) Batch
	NewIteratorWithRange(start, limit []byte) Iterator // 迭代器使用完需要release
	NewIteratorWithPrefix(prefix []byte) Iterator      // 迭代器使用完需要release
	NewList() Lister
}

// Batch Batch操作的接口
type Batch interface {
	// 缓存的记录条数
	ValueSize() int
	// 将记录从缓存写到db
	Write() error
	// 清空缓存
	Reset()
	// 将记录保存到缓存
	Set(key []byte, value []byte) error
	// 从缓存删除记录
	Delete(key []byte) error
}

// Iterator NewIteratorXX操作后得到的迭代器
type Iterator interface {
	Valid() bool
	Key() []byte
	Value() []byte
	Next() bool
	Prev() bool
	Last() bool
	First() bool
	Error() error
	Release() // 迭代器使用完需要release
}

// Lister 列表接口
type Lister interface {
	// range for [start, limit)
	ListRange(start, limit []byte, count int32, opt Option) ([][]byte, [][]byte, error)
	ListPrefix(prefix []byte, count int32, opt Option) ([][]byte, [][]byte, error)
	RangeCount(start, limit []byte) int64
	PrefixCount(prefix []byte) int64
}
